public class StateMachine<T>
{
	private T owner;

	private State<T> currentState;

	private State<T> preState;

	public float timeInState;

	public StateMachine(T _owner)
	{
		owner = _owner;
		currentState = null;
		preState = null;
	}

	public void update(float deltaTime)
	{
		if (currentState != null)
		{
			currentState.update(owner, deltaTime);
		}
		timeInState += deltaTime;
	}

	public void fixedUpdate()
	{
		if (currentState != null)
		{
			currentState.fixedUpdate(owner);
		}
	}

	public void initState(State<T> newState)
	{
		if (newState == null)
		{
			LogUtils.Error("null state");
			return;
		}
		currentState = newState;
		currentState.enter(owner);
		preState = null;
		timeInState = 0f;
	}

	public void changeState(State<T> newState)
	{
		if (newState == null)
		{
			LogUtils.Error("null state");
		}
		else if (newState != currentState)
		{
			preState = currentState;
			if (currentState != null)
			{
				currentState.exit(owner);
			}
			currentState = newState;
			if (currentState != null)
			{
				currentState.enter(owner);
			}
			timeInState = 0f;
		}
	}

	public void backToPreState()
	{
		if (preState == currentState)
		{
			LogUtils.Error("prestate == currentState");
		}
		changeState(preState);
	}

	public bool isInState(State<T> state)
	{
		if (state == currentState)
		{
			return true;
		}
		return false;
	}

	public bool isPreState(State<T> state)
	{
		if (preState == state)
		{
			return true;
		}
		return false;
	}

	public State<T> getCurrentState()
	{
		return currentState;
	}

	public bool handleBackPress()
	{
		if (currentState != null)
		{
			return currentState.onBackPress(owner);
		}
		return true;
	}

	public void handleHomePress()
	{
		if (currentState != null)
		{
			currentState.onHomePress(owner);
		}
	}
}
